package message

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Message struct {
	Status     int         `json:"status,omitempty"`
	Msg        string      `json:"message,omitempty"`
	ActivityId int64       `json:"activity_id,omitempty"`
	Token      string      `json:"token,omitempty"`
	Data       interface{} `json:"data,omitempty"`
}

const (
	SUCCESS  = 200
	ERROR    = 500
	NotFound = 404

	// 用户模块 1000...
	ErrorUserAdd = 1001
	//SuccessUserAdd        = 1017
	//SuccessUserLogin      = 1018
	ErrorUserEncrypt     = 1002
	ErrorCheckEmail      = 1004
	SuccessEmailExist    = 101
	SuccessEmailNotExist = 102
	ErrorCheckPhone      = 1005
	SuccessPhoneExist    = 100
	SuccessPhoneNotExist = 103
	ErrorCheckNick       = 1006
	SuccessNickExist     = 10
	SuccessNickNotExist  = 17
	ErrorUserPerfect     = 1007
	//SuccessUserPerfect    = 1021
	ErrorGetRole        = 1008
	ErrorAuthRole       = 1039
	ErrorUpdatePassword = 1009
	//SuccessUpdatePassword = 1042
	ErrorUserLogin         = 1010
	ErrorGetUserInfo       = 1011
	ErrorNoAuthKey         = 1012
	ErrorGetAuthCode       = 1013
	ErrorAddAuthCode       = 1014
	ErrorAuthCode          = 1015
	ErrorAuthCodeBeenSend  = 1016
	ErrorAuthCodeCount     = 1017
	ErrorAuthCodeConv      = 1018
	ErrorAuthUserCode      = 1027
	ErrorAuthUserNick      = 1030
	ErrorAuthUserEmail     = 1028
	ErrorAuthUserPassword  = 1029
	ErrorCheckUserAccount  = 1033
	ErrorCheckUserPassword = 1034
	ErrorUUIDNotExist      = 1035

	// 活动模块 2000...
	ErrorAddActivity        = 2001
	ErrorCheckActivity      = 2004
	SuccessActivityExist    = 2005
	SuccessActivityNotExist = 2006
	ErrorUpdateActivity     = 2007
	//SuccessUpdateActivity       = 2008
	ErrorGetActivityList = 2009
	//SuccessGetActivityList      = 2010
	ErrorActivityId             = 2011
	ErrorActivityDataNil        = 2012
	ErrorCheckActivityId        = 2013
	SuccessActivityIdExist      = 2014
	SuccessActivityIdNotExist   = 2015
	ErrorGetNewActivity         = 2016
	ErrorGetNewActivityNotExist = 2017

	// 参与模块 3000...
	ErrorJoinAdd        = 3001
	ErrorCheckJoin      = 3002
	SuccessJoinExist    = 3003
	SuccessJoinNotExist = 3004
	ErrorUpdateJoin     = 3005
	//SuccessUpdateJoin     = 3006
	ErrorGetJoinList = 3007
	ErrorJoinDataNil = 3008
	//SuccessJoinAdd        = 3009
	//SuccessGetJoinList    = 3010
	ErrorCheckJoinId      = 3011
	SuccessJoinIdExist    = 3012
	SuccessJoinIdNotExist = 3013

	// jwt模块 4000...
	ErrorGenToken     = 4001
	ErrorParseToken   = 4002
	ErrorInvalidToken = 4003
	ErrorHeaderNil    = 4004
	ErrorHeaderFormat = 4005
	ErrorTokenSession = 4006

	// 邮箱模块 5000
	ErrorEmailDial             = 5001
	ErrorEmailAuthUser         = 5002
	ErrorEmailCallMail         = 5003
	ErrorEmailCallRcpt         = 5004
	ErrorEmailCallData         = 5005
	ErrorEmailWriteStream      = 5006
	ErrorEmailCloseWriteStream = 5007
	ErrorEmailCloseServer      = 5008
	//SuccessEmailSendAuthCode   = 5009

	// 地理库模块 6000
	ErrorGeoGetOneProvince = 6000
	ErrorGeoGetTwoCity     = 6001
	ErrorGeoGetThreeCounty = 6002
	ErrorGeoGetFourTown    = 6003
	ErrorGeoGetOtherCity   = 6004
	//SuccessGeoGetOneProvince = 6005
	SuccessGeoGetTwoCity     = 6006
	SuccessGeoGetThreeCounty = 6007
	SuccessGeoGetFourTown    = 6008
	//SuccessGeoGetOtherCity   = 6009

	// 其他
	ErrorBindData        = 8001
	ErrorDataFormat      = 8002
	ErrorGetRowsAffected = 1003
	ErrorGetLastInsertId = 2002
)

var codeMsg = map[int]string{
	SUCCESS:  "OK",
	ERROR:    "FAIL",
	NotFound: "NotFount",

	// 用户模块
	ErrorUserAdd: "注册用户失败",
	//SuccessUserAdd:        "注册用户成功",
	//SuccessUserLogin:      "登录用户成功",
	ErrorUserEncrypt:     "密码加密失败",
	ErrorCheckEmail:      "检查邮箱失败",
	SuccessEmailExist:    "邮箱号已被注册",
	SuccessEmailNotExist: "邮箱号未注册",
	ErrorCheckPhone:      "检查手机号失败",
	SuccessPhoneExist:    "手机号已被注册",
	SuccessPhoneNotExist: "手机号未注册",
	ErrorCheckNick:       "检查昵称失败",
	SuccessNickExist:     "昵称已被注册",
	SuccessNickNotExist:  "昵称未注册",
	ErrorUserPerfect:     "完善用户信息失败",
	//SuccessUserPerfect:    "完善信息成功",
	ErrorGetRole:        "获取角色权限失败",
	ErrorAuthRole:       "用户无权限",
	ErrorUpdatePassword: "修改密码失败",
	//SuccessUpdatePassword: "修改密码成功",
	ErrorUserLogin:         "账户或密码错误",
	ErrorGetUserInfo:       "获取用户信息失败",
	ErrorNoAuthKey:         "验证码错误",
	ErrorGetAuthCode:       "查找验证码失败",
	ErrorAddAuthCode:       "新增验证码失败",
	ErrorAuthCode:          "验证码错误,请检查后输入",
	ErrorAuthCodeBeenSend:  "验证码已发送,请稍后在试",
	ErrorAuthCodeCount:     "验证码发送次数达到限制,请稍后在试",
	ErrorAuthCodeConv:      "验证码转换失败",
	ErrorAuthUserCode:      "验证码长度不正确",
	ErrorAuthUserNick:      "昵称格式错误",
	ErrorAuthUserEmail:     "邮箱格式错误",
	ErrorAuthUserPassword:  "密码格式错误",
	ErrorCheckUserAccount:  "账户错误",
	ErrorCheckUserPassword: "密码错误",
	ErrorUUIDNotExist:      "未获取到uuid",

	// 活动模块
	ErrorAddActivity:        "新增活动信息失败",
	ErrorCheckActivity:      "检查活动信息失败",
	SuccessActivityExist:    "活动信息已存在",
	SuccessActivityNotExist: "活动信息不存在",
	ErrorUpdateActivity:     "修改活动信息失败",
	//SuccessUpdateActivity:       "更新活动信息成功",
	ErrorGetActivityList: "获取活动列表失败",
	//SuccessGetActivityList:      "获取活动列表成功",
	ErrorActivityId:             "活动ID数据错误",
	ErrorActivityDataNil:        "未获取到活动列表",
	ErrorCheckActivityId:        "检查活动ID失败",
	SuccessActivityIdExist:      "活动ID存在",
	SuccessActivityIdNotExist:   "活动ID不存在",
	ErrorGetNewActivity:         "获取最新活动失败",
	ErrorGetNewActivityNotExist: "未获取到最新活动",

	// 参与模块
	ErrorJoinAdd:        "参与活动失败",
	ErrorCheckJoin:      "检查参与状态失败",
	SuccessJoinExist:    "已参与该活动",
	SuccessJoinNotExist: "未参与该活动",
	ErrorUpdateJoin:     "修改参与状态失败",
	//SuccessUpdateJoin:     "修改参与状态成功",
	ErrorGetJoinList: "获取参与列表失败",
	ErrorJoinDataNil: "未获取到参与列表",
	//SuccessJoinAdd:        "参与活动成功",
	//SuccessGetJoinList:    "获取参与列表成功",
	ErrorCheckJoinId:      "检查参与ID失败",
	SuccessJoinIdExist:    "参与ID存在",
	SuccessJoinIdNotExist: "参与ID不存在",

	// jwt模块
	ErrorGenToken:     "生成Token失败",
	ErrorParseToken:   "验证Token失败",
	ErrorInvalidToken: "无效Token",
	ErrorHeaderNil:    "请求头Auth为空",
	ErrorHeaderFormat: "请求头Auth格式有误",
	ErrorTokenSession: "Token已过期",

	// 邮箱模块
	ErrorEmailDial:             "连接邮箱服务器失败",
	ErrorEmailAuthUser:         "邮箱账户验证失败",
	ErrorEmailCallMail:         "邮箱账户呼叫服务器Mail失败",
	ErrorEmailCallRcpt:         "邮箱账户呼叫服务器Rcpt失败",
	ErrorEmailCallData:         "邮箱账户呼叫服务器Data失败",
	ErrorEmailWriteStream:      "邮箱数据流写入失败",
	ErrorEmailCloseWriteStream: "关闭邮箱写入流失败",
	ErrorEmailCloseServer:      "关闭邮箱服务器失败",
	//SuccessEmailSendAuthCode:   "发送验证码成功",

	// 地理库模块
	ErrorGeoGetOneProvince: "获取第一级省市失败",
	ErrorGeoGetTwoCity:     "获取第二级州市失败",
	ErrorGeoGetThreeCounty: "获取第三级区县失败",
	ErrorGeoGetFourTown:    "获取第四级乡镇失败",
	ErrorGeoGetOtherCity:   "获取城市列表失败",
	//SuccessGeoGetOneProvince: "获取第一级省市成功",
	SuccessGeoGetTwoCity:     "获取第二级州市成功",
	SuccessGeoGetThreeCounty: "获取第三级区县成功",
	SuccessGeoGetFourTown:    "获取第四级乡镇成功",
	//SuccessGeoGetOtherCity:   "获取城市列表成功",

	// 其他
	ErrorBindData:        "绑定数据失败",
	ErrorDataFormat:      "数据格式错误",
	ErrorGetRowsAffected: "获取操作影响行数失败",
	ErrorGetLastInsertId: "获取最后插入行ID失败",
}

func GetMsg(code int) string {
	return codeMsg[code]
}
func SendMsg(c *gin.Context, code int, args ...interface{}) {
	var msg Message
	msg.Status = code
	msg.Msg = GetMsg(code)
	// 遍历可变参数切片
	for _, s := range args {
		// 断言参数类型
		switch ins := s.(type) {
		case int64:
			msg.ActivityId = ins
		case string:
			msg.Token = ins
		case interface{}:
			msg.Data = ins
		}
	}
	c.JSON(http.StatusOK, msg)
}
